package com.example.saiweisi.service.impl.loginService;

import com.example.saiweisi.controller.dto.AdminDTO;
import com.example.saiweisi.controller.request.LoginRequest;
import com.example.saiweisi.exception.ServiceException;
import com.example.saiweisi.mapper.loginMapper.AdminLoginMapper;
import com.example.saiweisi.model.ReceptionAdmin;
import com.example.saiweisi.model.SystemAdmin;
import com.example.saiweisi.model.ChefAdmin;
import com.example.saiweisi.model.WaiterAdmin;
import com.example.saiweisi.service.interf.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService implements ILoginService{

    @Autowired
    AdminLoginMapper adminLoginMapper;

    @Override
    public AdminDTO login(LoginRequest request) {
        //截取前端输入账号的第一位，来判断输入的是什么类型的管理员
        String firstOfNum = request.getNum().substring(0, 1);
        AdminDTO adminDTO = new AdminDTO();
        if("1".equals(firstOfNum)){
            SystemAdmin systemAdmin= adminLoginMapper.selectSystemAdmin(request);
            if(systemAdmin==null){
                throw new ServiceException("用户名或密码错误！");
            }
            adminDTO.setPower(1);
            adminDTO.setAdminName(systemAdmin.getSystemAdminName());
            adminDTO.setAdminNum(systemAdmin.getSystemAdminNum());
        }else if("2".equals(firstOfNum)){
            ReceptionAdmin receptionAdmin= adminLoginMapper.selectReceptionAdmin(request);
            if(receptionAdmin==null){
                throw new ServiceException("用户名或密码错误！");
            }
            adminDTO.setPower(2);
            adminDTO.setAdminNum(receptionAdmin.getReceptionAdminNum());
            adminDTO.setAdminName(receptionAdmin.getReceptionAdminName());
            adminDTO.setAdminId(receptionAdmin.getReceptionAdminId());
            adminDTO.setAdminTel(receptionAdmin.getReceptionAdminTel());
        }else if("3".equals(firstOfNum)){
            ChefAdmin chefAdmin = adminLoginMapper.selectChefAdmin(request);
            if(chefAdmin ==null){
                throw new ServiceException("用户名或密码错误！");
            }
            adminDTO.setPower(3);
            adminDTO.setAdminNum(chefAdmin.getChefAdminNum());
            adminDTO.setAdminName(chefAdmin.getChefAdminName());
            adminDTO.setAdminId(chefAdmin.getChefAdminId());
            adminDTO.setAdminTel(chefAdmin.getChefAdminTel());
        }else if("4".equals(firstOfNum)){
            WaiterAdmin waiterAdmin = adminLoginMapper.selectWaiterAdmin(request);
            if(waiterAdmin ==null){
                throw new ServiceException("用户名或密码错误！");
            }
            adminDTO.setPower(4);
            adminDTO.setAdminNum(waiterAdmin.getWaiterAdminNum());
            adminDTO.setAdminName(waiterAdmin.getWaiterAdminName());
            adminDTO.setAdminId(waiterAdmin.getWaiterAdminId());
            adminDTO.setAdminTel(waiterAdmin.getWaiterAdminTel());
        }else {
            throw new ServiceException("工号输入格式不正确！");
        }

        return adminDTO;
    }
}
